package com.bawei.lib_storage.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.bawei.lib_storage.room.entity.FavoriteEntity

@Dao
interface FavoriteDao {
    
    /**
     * 获取所有收藏
     */
    @Query("SELECT * FROM b_favorite ORDER BY createTime DESC")
    fun getAll(): List<FavoriteEntity>
    
    /**
     * 根据用户ID获取收藏列表
     */
    @Query("SELECT * FROM b_favorite WHERE userId = :userId ORDER BY createTime DESC")
    fun getAllByUserId(userId: String): List<FavoriteEntity>
    
    /**
     * 根据用户ID分页获取收藏列表
     */
    @Query("SELECT * FROM b_favorite WHERE userId = :userId ORDER BY createTime DESC LIMIT (:page * :pageSize), :pageSize")
    fun getAllByUserIdAndPage(userId: String, page: Int, pageSize: Int): List<FavoriteEntity>
    
    /**
     * 根据用户ID和视频ID检查是否已收藏
     */
    @Query("SELECT COUNT(*) FROM b_favorite WHERE userId = :userId AND videoId = :videoId")
    fun isFavorite(userId: String, videoId: String): Int
    
    /**
     * 根据用户ID和视频ID获取收藏记录
     */
    @Query("SELECT * FROM b_favorite WHERE userId = :userId AND videoId = :videoId LIMIT 1")
    fun getFavoriteByUserIdAndVideoId(userId: String, videoId: String): FavoriteEntity?
    
    /**
     * 根据关键词搜索收藏
     */
    @Query("SELECT * FROM b_favorite WHERE userId = :userId AND (title LIKE '%' || :keywords || '%' OR content LIKE '%' || :keywords || '%') ORDER BY createTime DESC")
    fun searchByKeywords(userId: String, keywords: String): List<FavoriteEntity>
    
    /**
     * 插入收藏
     */
    @Insert
    fun insert(favorite: FavoriteEntity)
    
    /**
     * 删除收藏
     */
    @Delete
    fun delete(favorite: FavoriteEntity)
    
    /**
     * 根据用户ID和视频ID删除收藏
     */
    @Query("DELETE FROM b_favorite WHERE userId = :userId AND videoId = :videoId")
    fun deleteByUserIdAndVideoId(userId: String, videoId: String)
    
    /**
     * 删除用户所有收藏
     */
    @Query("DELETE FROM b_favorite WHERE userId = :userId")
    fun deleteAllByUserId(userId: String)
    
    /**
     * 删除所有收藏
     */
    @Query("DELETE FROM b_favorite")
    fun deleteAll()
} 